package com.epalmpay.controller.ys.oilcard;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.bean.FlowChargeOrderBean;
import com.epalmpay.bean.OilCardDenominationBean;
import com.epalmpay.bean.OilcardChargeOrderBean;
import com.epalmpay.bean.OilcardRechargeInfoBean;
import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;

import com.epalmpay.dto.oilcard.OilcardInDTO;
import com.epalmpay.dto.oilcard.OilcardListOutDTO;
import com.epalmpay.dto.oilcard.OilcardOutDTO;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.yunliu.oilcard.DenominationListOutDTO;
import com.epalmpay.dto.yunliu.oilcard.FlowOrderListInDTO;
import com.epalmpay.dto.yunliu.oilcard.FlowOrderListOutDTO;
import com.epalmpay.dto.yunliu.oilcard.OilCardOrderReChargeInDTO;
import com.epalmpay.dto.yunliu.oilcard.OilcarReqChargeListOutDTO;
import com.epalmpay.dto.yunliu.oilcard.OilcardChargeEditOutDTO;
import com.epalmpay.dto.yunliu.oilcard.OilcardChargeInfoListDTO;
import com.epalmpay.dto.yunliu.oilcard.OilcardChargeInfoOutDTO;
import com.epalmpay.dto.yunliu.oilcard.OilcardChargeListInDTO;
import com.epalmpay.dto.yunliu.oilcard.OilcardChargeListOutDTO;
import com.epalmpay.dto.yunliu.oilcard.BaseOilOrderInDTO;
import com.epalmpay.dto.yunliu.oilcard.CancelOilChargeInDTO;
import com.epalmpay.dto.yunliu.oilcard.OilcardOrderBaseOutDTO;

import com.epalmpay.entity.Page;
import com.epalmpay.entity.YsOilcard;
import com.epalmpay.entity.YsOilcardOrder;
import com.epalmpay.entity.YsOilcardRechargeInfo;
import com.epalmpay.entity.YsOilcardRepOrder;
import com.epalmpay.service.yunstream.IOilCardService;
import com.epalmpay.service.yunstream.IOilcardOrderService;
import com.epalmpay.util.PropertyCopyUtil;
import com.epalmpay.util.Tools;
import com.epalmpay.util.SysUtils;


@Controller
@RequestMapping(value="/merchant/oilcard")
public class YsOilcardController extends BaseController  {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger logger = LoggerFactory.getLogger(YsOilcardController.class);
	
	
	@Resource
	private IOilcardOrderService oilcardOrderService;
	
	@Resource
	private IOilCardService oilCardService;
	
	
	/**
	 * 油卡列表
	 * @param oilcardInDTO
	 * @param pageDTO
	 * @return
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public OilcardListOutDTO  oilacrdList(OilcardInDTO oilcardInDTO,PageDTO pageDTO){
		OilcardListOutDTO oilcardListOutDTO=new OilcardListOutDTO();
		try {
			Page page=Tools.pageEmtity(pageDTO);
			
			oilcardListOutDTO = oilCardService.getOilcardList(oilcardInDTO,page);			
			oilcardListOutDTO.setPage(page); 
			oilcardListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			
			return oilcardListOutDTO;
		} catch (ApplicationException ae) { 
			oilcardListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 oilcardListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return oilcardListOutDTO;
	}
	
	

	/**
	 * 修改状态
	 * @param oilcardInDTO
	 * @return
	 */
	@RequestMapping(value="/updateStatus")
	@ResponseBody
	public OilcardOutDTO  updateStatus(OilcardInDTO oilcardInDTO){
		OilcardOutDTO oilcardOutDTO=new OilcardOutDTO();
		try {  
			YsOilcard ysOilcard=oilCardService.findOilcardStatus(oilcardInDTO.getId());
			oilCardService.updateStatus(ysOilcard);
			
			oilcardOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
			return oilcardOutDTO;
		} catch (Exception e) { 
			oilcardOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 删除油卡
	 */
	@RequestMapping(value="/deleteOilcard")
	@ResponseBody
	public OilcardOutDTO  deleteOilcard(OilcardInDTO oilcardInDTO){
		
		OilcardOutDTO oilcardOutDTO=new OilcardOutDTO();
	
		try {  
			
			oilcardOutDTO=oilCardService.deleteOilcard(oilcardInDTO.getId()); 
			
			return oilcardOutDTO;
		} catch (Exception e) { 
			oilcardOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 油卡套餐新增保存
	 */
	@RequestMapping(value="/saveOilcard")
	@ResponseBody
	public OilcardOutDTO  saveOilcard(OilcardInDTO oilcardInDTO){
		OilcardOutDTO oilcardOutDTO = new OilcardOutDTO();
		try { 
            //保存会员等级
			 oilcardOutDTO=oilCardService.saveOilcard(oilcardInDTO);
			
			return oilcardOutDTO;
		} catch (ApplicationException ae) { 
			oilcardOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 oilcardOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return oilcardOutDTO;
	}


	/**
	 *
	 * @param oilcardInDTO
	 * @return
	 */
	@RequestMapping(value="/goEditOilcard")
	@ResponseBody
	public OilcardOutDTO  goEditOilcard(OilcardInDTO oilcardInDTO){
		OilcardOutDTO oilcardOutDTO = new OilcardOutDTO();
		YsOilcard oilcard=new YsOilcard();
		try {   
			oilcard.setId(oilcardInDTO.getId());
			YsOilcard  yoc =oilCardService.findOilcardById(oilcard);
			if(yoc!=null){
				PropertyCopyUtil.copyPropertiesExclude(yoc,oilcardOutDTO);
				oilcardOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功");
				return oilcardOutDTO;
			}else{
				oilcardOutDTO.setErrorCode("-1", "未查询到记录");
			}
		} catch (Exception e) { 
			oilcardOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		return oilcardOutDTO;
	}
	
	/**
	 * 编辑油卡套餐 保存
	 * @param oilcardInDTO
	 * @return
	 */
	@RequestMapping(value="/editOilcard")
	@ResponseBody
	public OilcardOutDTO editOilcard(OilcardInDTO oilcardInDTO){
		
		OilcardOutDTO olcardOutDTO=new OilcardOutDTO();
		try { 
			olcardOutDTO=oilCardService.saveOilcard(oilcardInDTO);
			
			return olcardOutDTO;
		} catch (Exception e) { 
			olcardOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		return olcardOutDTO;
		
	}

	
	/**
	 * 查询会员等级晋级金额是否存在
	 * 
	 */
	@RequestMapping(value="/queryData")
	@ResponseBody
	public OilcardOutDTO  queryData(OilcardInDTO oilcardInDTO){
		OilcardOutDTO oilcardOutDTO = new OilcardOutDTO();
		
		try {   
			oilcardOutDTO=oilCardService.queryData(oilcardInDTO);
			oilcardOutDTO.setErrorCode(Rescode.SUCCESS,"处理成功");
			return oilcardOutDTO;
			
		} catch (Exception e) { 
			oilcardOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		return oilcardOutDTO;
	}
	
	
	
	@RequestMapping("cancelOilCharge")
	@ResponseBody
	 public BaseOutDTO cancelOilCharge(CancelOilChargeInDTO canceOilDTO){ 
  
		BaseOutDTO baseOutDTO=new BaseOutDTO();
		try {   
			if(canceOilDTO.getOilOrderId()==null||StringUtils.isBlank(canceOilDTO.getOilchargeOrderIds())){
				baseOutDTO.setErrorCode("-1", "主要参数不能为空");
				return baseOutDTO;
			}
			String msg=oilcardOrderService.finishCancelOilCharge(canceOilDTO);
			if(msg!=null){
				baseOutDTO.setErrorCode("-1", msg);
			}else{
				baseOutDTO.setErrorCode(Rescode.SUCCESS,"处理成功");
			}
			
		} catch (Exception e) { 
			baseOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		
		return baseOutDTO;
	}
	
	
	@RequestMapping("oilcardChargeOrderList")
	@ResponseBody
	 public OilcardChargeListOutDTO oilcardChargeOrderList(OilcardChargeListInDTO listInDTO,PageDTO pageDTO){ 
		
		OilcardChargeListOutDTO oilcardChargeListDTO=new OilcardChargeListOutDTO();
		Page page=Tools.pageEmtity(pageDTO);
		List<OilcardChargeOrderBean> oilcardChargelList=this.oilcardOrderService.listOilcardCharge(listInDTO,page);
		oilcardChargeListDTO.setOilcardChargelList(oilcardChargelList);
		List<OilCardDenominationBean> result=this.oilCardService.listOilcardDenomination(listInDTO.getOilcardType());
		oilcardChargeListDTO.setOilCardListDenomination(result);
		oilcardChargeListDTO.setPage(page); 
		oilcardChargeListDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
       return oilcardChargeListDTO;
	}
	
	/**
	 * 用户根据用户用户油卡编号查询
	 * @param listInDTO
	 * @param pageDTO
	 * @return
	 */
	@RequestMapping("oilcardChargeInfoList")
	@ResponseBody
	 public OilcardChargeInfoListDTO oilcardChargeInfoList(OilCardOrderReChargeInDTO listInDTO,PageDTO pageDTO){ 
		
		OilcardChargeInfoListDTO oilcardChargeListDTO=new OilcardChargeInfoListDTO();
		Page page=Tools.pageEmtity(pageDTO);
		List<OilcardRechargeInfoBean> oilcardChargelList=this.oilcardOrderService.listOilcardChargeByMemberCardId(listInDTO,page);
		oilcardChargeListDTO.setOilcardChargelList(oilcardChargelList);
		
		oilcardChargeListDTO.setPage(page); 
		oilcardChargeListDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
       return oilcardChargeListDTO;
	}
	
		
	@RequestMapping(value = "/oilCardDenominationList")
	@ResponseBody
	public DenominationListOutDTO  oilCardDenominationList(BaseOilOrderInDTO listInDTO){  
		DenominationListOutDTO denominationListDTO=new DenominationListOutDTO();
		String oilcardType=listInDTO.getOilcardType();
     	List<OilCardDenominationBean> result=this.oilCardService.listOilcardDenomination(oilcardType);
     	denominationListDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
     	denominationListDTO.setDenominationList(result);
    	 return denominationListDTO;
	}
	
	
	@RequestMapping(value = "/flowOrderNotify")
	@ResponseBody
	public BaseOutDTO  flowOrderNotify(FlowOrderListInDTO listInDTO){  
		BaseOutDTO baseOutDTO=new BaseOutDTO();
		if(listInDTO.getId()==null){
			baseOutDTO.setErrorCode("002X", "参数不能为空");
			return baseOutDTO;
		}
		baseOutDTO=oilcardOrderService.finishFlowsPayNotify(listInDTO.getId());
    	 return baseOutDTO;
	}
	
	@RequestMapping(value = "/flowOrderList")
	@ResponseBody
	public FlowOrderListOutDTO  flowOrderList(FlowOrderListInDTO listInDTO,PageDTO pageDTO){  
		FlowOrderListOutDTO baseOutDTO=new FlowOrderListOutDTO();
		if(listInDTO.getGroupId()==null){
			baseOutDTO.setErrorCode("002X", "参数不能为空");
			return baseOutDTO;
		}
		Page page=Tools.pageEmtity(pageDTO);
		List<FlowChargeOrderBean> flowChargeList=this.oilcardOrderService.listFlowsCharge(listInDTO,page);
		baseOutDTO.setFlowChargeList(flowChargeList);
		baseOutDTO.setPage(page); 
		baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
		
    	 return baseOutDTO;
	}
	
	
	
	

	//分页查询补充订单
	@ResponseBody
	@RequestMapping("repListPage")
	public OilcarReqChargeListOutDTO  repListPage(BaseOilOrderInDTO listInDTO,PageDTO pageDTO){
		
		OilcarReqChargeListOutDTO oilcardReqChargeListDTO=new OilcarReqChargeListOutDTO();
       Page page=Tools.pageEmtity(pageDTO);
       List<YsOilcardRepOrder> oilcardReqChargelList=this.oilcardOrderService.selectByOilcardOrderId(listInDTO,page);
		
       oilcardReqChargeListDTO.setOilcardReqChargelList(oilcardReqChargelList);
       oilcardReqChargeListDTO.setPage(page); 
       oilcardReqChargeListDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
       return oilcardReqChargeListDTO;
		}
	
	@RequestMapping("update")
	public OilcardOrderBaseOutDTO update(BaseOilOrderInDTO orderInDTO){
		OilcardOrderBaseOutDTO outDTO=new OilcardOrderBaseOutDTO();
		YsOilcardOrder oilcardOrder=oilcardOrderService.finishOidCardOrderById(orderInDTO.getOrderId());
		if(oilcardOrder!=null){
			try {
				PropertyCopyUtil.copyPPropertiesNotNull(oilcardOrder, outDTO);
			} catch (Exception e) {
				e.printStackTrace();
			}
			outDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
		}else{
			outDTO.setErrorCode("0987", "查询信息失败");
		}
		
	    return outDTO;
		
	}
	
////导出
//@RequestMapping(value="/exportEx")
//public String exportEx(HttpServletResponse response,HttpServletRequest request,Model model){
//	logger.info("导出order到excel");
//	//LoginUser user =this.getLoginUser();
//	String oilcardType=getRequestParamString("oilcardType",null);
//	String datefrom=getRequestParamString("datefrom",null);
//	String dateto=getRequestParamString("dateto",null);
//	String denomination=getRequestParamString("denomination",null);
//	String username=getRequestParamString("username",null);//用户信息
//	String paymentStatus=getRequestParamString("paymentStatus",null);
//	String rechargeStatus=getRequestParamString("rechargeStatus", null);
//	
//	ModelAndView mv = new ModelAndView();
//	try{
//		String[] titles=new String[]{"会员账号","会员名称","充值费用","实付费用","油卡账号","油卡类型","支付状态","支付方式","支付时间","是否提交充值","充值状态"};
//		String []columnNames=new String[]{"var1","var2","var3","var4","var5","var6","var7","var8","var9","var10","var11"};
//		 ArrayList<Map<String,Object>> varList=new ArrayList<Map<String,Object>>();
//		List<Map<String,Object>> queryContentList =  oilcardOrderService.listOilcardCharge(null, oilcardType, datefrom, username, dateto, denomination, paymentStatus, rechargeStatus);
//		
//		for(int i=0;i<queryContentList.size();i++){
//			
//			Map<String,Object> order = queryContentList.get(i);
//			Map<String,Object>	vpd=new HashMap<String,Object>();
//			vpd.put("var1",order.get("merberAcount")+"" );	//1
//			vpd.put("var2", order.get("name")+"");	//2
//			vpd.put("var3", SysUtils.fenToYuan(SysUtils.Object2int(order.get("rechargeAmount"))));	//3
//			vpd.put("var4", SysUtils.fenToYuan(SysUtils.Object2int(order.get("PaymentAmount"))));	//4
//			vpd.put("var5",  order.get("OilcardNumber")+"");	//5
//			String toilcardType = order.get("OilcardType")+"";
//			if("ZSH".equals(toilcardType)){//6
//				vpd.put("var6", "中石化");
//			}else if("ZSY".equals(toilcardType)){
//				vpd.put("var6", "中石油");
//			}else {
//				vpd.put("var6", "--");
//			}
//			//7支付状态
//			Integer tpaymentStatus = SysUtils.Object2int(order.get("PaymentStatus"));
//			if(paymentStatus!=null&&1 == tpaymentStatus){
//				vpd.put("var7", "未支付");
//			}else if(paymentStatus!=null&&2 == tpaymentStatus){
//				vpd.put("var7", "已支付");
//			}else {
//				vpd.put("var7", "--");
//			}
//			//8支付方式
//			Integer paymentType = SysUtils.Object2int(order.get("PaymentType"));
//			if(paymentType!=null && 1 == paymentType){
//				vpd.put("var8", "支付宝");
//			}else if(paymentType!=null && 2 == paymentType){
//				vpd.put("var8", "微信");
//			}else if(paymentType!=null && 3 == paymentType){
//				vpd.put("var8", "银行卡");
//			}else if(paymentType!=null && 4 == paymentType){
//				vpd.put("var8", "余额");
//			}else{
//				vpd.put("var8", "其他");
//			}
//			//8支付时间
//			Date paymentTime = (Date)order.get("PaymentTime");
//			if(paymentTime!=null){
//			vpd.put("var9", paymentTime.toLocaleString());
//			}else{
//				vpd.put("var9", "--");
//			}
//			//9充值状态 1、发送充值请求成功；2、发送充值请求失败；3、充值成功；4、充值失败
//			int trechargeStatus = SysUtils.Object2int(order.get("RechargeStatus")); 
//			if(trechargeStatus==1||trechargeStatus==3||trechargeStatus==4){
//				vpd.put("var10", "是");
//			}else if(trechargeStatus==2){
//				vpd.put("var10", "否"); 
//			}else{
//				vpd.put("var10", "--");
//			}
//			
//			if(rechargeStatus!=null&&3 == trechargeStatus){
//				vpd.put("var11", "成功");
//			}else if(rechargeStatus!=null&&4 == trechargeStatus&&7 == trechargeStatus){
//				vpd.put("var11", "失败");
//			}else {
//				vpd.put("var11", "--");
//			}
//			
//			varList.add(vpd);
//		}
//		
//		ByteArrayOutputStream os = new ByteArrayOutputStream();
//        try {
//            ExcelUtil.createWorkBook(varList,titles,columnNames).write(os);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        byte[] content = os.toByteArray();
//        InputStream is = new ByteArrayInputStream(content);
//        // 设置response参数，可以打开下载页面
//        response.reset();
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");
//        String fileName=java.net.URLEncoder.encode("油卡充值列表", "UTF-8");
//        response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
//        ServletOutputStream out = response.getOutputStream();
//        BufferedInputStream bis = null;
//        BufferedOutputStream bos = null;
//        try {
//            bis = new BufferedInputStream(is);
//            bos = new BufferedOutputStream(out);
//            byte[] buff = new byte[2048];
//            int bytesRead;
//            // Simple read/write loop.
//            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
//                bos.write(buff, 0, bytesRead);
//            }
//        } catch (final IOException e) {
//            throw e;
//        } finally {
//            if (bis != null)
//                bis.close();
//            if (bos != null)
//                bos.close();
//        }
//	} catch(Exception e){
//		logger.error(e.toString(), e);
//	}
//	return null;
//}




@RequestMapping("oilcardOrderInfo")
@ResponseBody
 public OilcardChargeInfoOutDTO oilcardOrderInfo(BaseOilOrderInDTO oilOrderInDTO,PageDTO pageDTO){ 
	OilcardChargeInfoOutDTO outDTO=new OilcardChargeInfoOutDTO();
	Long orderId=oilOrderInDTO.getOrderId();
	
	YsOilcardOrder oilcardOrder=oilcardOrderService.findOilChargeOrderInfo(orderId);
	outDTO.setOilcardOrder(oilcardOrder);
	YsOilcard oilcard=oilCardService.finishOidCardById(oilcardOrder.getOilcardPackageId());
	outDTO.setOilcard(oilcard);
	if(oilcardOrder.getOrderPackageType().intValue()==0){
		try {
			
			if(oilcardOrder.getPaymentStatus().intValue()==2){
				YsOilcardRechargeInfo oilcardRechargeInfo = oilcardOrderService.findOilcardRechargeInfoByOid(oilcardOrder.getId());
				
				outDTO.setOilcardRechargeInfo(oilcardRechargeInfo);
				Page page=Tools.pageEmtity(pageDTO);
				
				List<YsOilcardRepOrder> oilcardReqChargelList=this.oilcardOrderService.selectByOilcardReOrderId(page,oilcardRechargeInfo.getId());		
				outDTO.setOilcardReqChargelList(oilcardReqChargelList);
				outDTO.setPage(page); 
				
			}
			outDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			
			return outDTO;
		} catch (Exception e) {		
			e.printStackTrace();
			outDTO.setErrorCode("0031");
			outDTO.setErrorMsg("数据异常：订单号："+oilcardOrder.getOilcardOrdernum());
			return 	outDTO;
		}
		
	}else{
		List<OilcardRechargeInfoBean> oilcardRechargeInfos=oilcardOrderService.findPackageORechargeInfoByOid(oilcardOrder.getId());
		outDTO.setOilcardRechargeInfos(oilcardRechargeInfos);
		outDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
		return outDTO;
	}
 	
}

	/**
	 *
	 * @param oilOrderInDTO
	 * @param pageDTO
	 * @return
	 */
	@RequestMapping("rechargeOrderInfo")
@ResponseBody
public OilcardChargeInfoOutDTO rechargeOrderInfo(BaseOilOrderInDTO oilOrderInDTO,PageDTO pageDTO){ 
	
	OilcardChargeInfoOutDTO outDTO=new OilcardChargeInfoOutDTO();
	Long orderId=oilOrderInDTO.getOrderId();
	Long oilCardChargeId=oilOrderInDTO.getOilCardChargeId();
	
	
	if(oilCardChargeId==null||orderId==null){
		 outDTO.setErrorCode("0032");
		 outDTO.setErrorMsg("参数缺失");
		return 	outDTO;
	}
	
	YsOilcardOrder oilcardOrder=oilcardOrderService.findOilChargeOrderInfo(orderId);
	outDTO.setOilcardOrder(oilcardOrder);
	YsOilcard ysOilcard=oilCardService.finishOidCardById(oilcardOrder.getOilcardPackageId());
	outDTO.setOilcard(ysOilcard);

	YsOilcardRechargeInfo oilcardRechargeInfo =oilcardOrderService.findOilReChargeOrderInfo(oilCardChargeId);
	outDTO.setOilcardRechargeInfo(oilcardRechargeInfo);
	
	 Page page=Tools.pageEmtity(pageDTO);
	
	List<YsOilcardRepOrder> oilcardReqChargelList=this.oilcardOrderService.selectByOilcardReOrderId(page,oilcardRechargeInfo.getId());
	outDTO.setOilcardReqChargelList(oilcardReqChargelList);
	outDTO.setPage(page); 
	outDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
    return outDTO;
	
}

@RequestMapping("oilcardChargeEdit")
@ResponseBody
 public OilcardChargeEditOutDTO oilcardChargeEdit(BaseOilOrderInDTO baseOilOrderInDTO ,PageDTO pageDTO){ 
	OilcardChargeEditOutDTO outDto=new OilcardChargeEditOutDTO();
	
	Long orderId=baseOilOrderInDTO.getOrderId();
	
	YsOilcardOrder oilcardOrder=oilcardOrderService.findOilChargeOrderInfo(orderId);
	outDto.setOilcardOrder(oilcardOrder);
	
	YsOilcardRechargeInfo oilcardRechargeInfo =null;
   if(oilcardOrder.getOrderPackageType().intValue()==0){
	   try {
			 oilcardRechargeInfo = oilcardOrderService.findOilcardRechargeInfoByOid(oilcardOrder.getId());
			 outDto.setOilcardRechargeInfo(oilcardRechargeInfo);
		} catch (Exception e) {
			e.printStackTrace();
			  
			  outDto.setErrorCode("0031");
			  outDto.setErrorMsg("数据异常：订单号："+oilcardOrder.getOilcardOrdernum());
				return 	outDto;
		}
   }else{
	  // 月套餐，查看某月的充值失败，需要修改参数进行补充值
	   
	   Long oilCardChargeId=baseOilOrderInDTO.getOilCardChargeId();;
	   if(oilCardChargeId==null){
		 
		   outDto.setErrorCode("0031");
			 outDto.setErrorMsg("oilCardChargeId：参数丢失");
				return 	outDto;
	   }
	   oilcardRechargeInfo = oilcardOrderService.findOilReChargeOrderInfo(oilCardChargeId);
	   outDto.setOilcardRechargeInfo(oilcardRechargeInfo);
   }
   Page page=Tools.pageEmtity(pageDTO);
   
    List<YsOilcardRepOrder> oilcardReqChargelList=this.oilcardOrderService.selectByOilcardReOrderId(page,oilcardRechargeInfo.getId());
	outDto.setOilcardReqChargelList(oilcardReqChargelList);
	outDto.setPage(page); 
    outDto.setErrorCode(Rescode.SUCCESS, "处理成功");
    return outDto;
 	
}




//补充值
@ResponseBody
@RequestMapping("oilCardOrderRecharge")
public BaseOutDTO oilCardOrderRecharge(OilCardOrderReChargeInDTO orderReChargeInDTO  ){
	
	Long orderId=orderReChargeInDTO.getOrderId();
	Long oilCardChargeId=orderReChargeInDTO.getOilCardChargeId();
	String oilcardNumber=orderReChargeInDTO.getOilcardNumber();
	String oilcardOwner=orderReChargeInDTO.getOilcardOwner();
	String oilcardType=orderReChargeInDTO.getOilcardType();
	String userName=orderReChargeInDTO.getOperaterUser();
	logger.info("订单号："+orderId+"补充油卡充值开始");
	BaseOutDTO baseOutDTO=new BaseOutDTO();
	YsOilcardOrder order = oilcardOrderService.finishOidCardOrderById(orderId);
	
	if(order==null){
		logger.info(orderId+" 订单不存在！");
		baseOutDTO.setErrorCode("0031");
		baseOutDTO.setErrorMsg("订单不存在！");
		return 	baseOutDTO;
	}
	//判断订单支付状态
	YsOilcardRechargeInfo oilcardRechargeInfo=null;
	if(order.getPaymentStatus() == 1){
		baseOutDTO.setErrorCode("0032");
		baseOutDTO.setErrorMsg("订单未支付，不能充值!");
		return 	baseOutDTO;
		
	}
	if(oilCardChargeId==null&&order.getOrderPackageType().intValue()==0){
		try {
			 oilcardRechargeInfo = oilcardOrderService.findOilcardRechargeInfoByOid(order.getId());
		} catch (Exception e) {
			e.printStackTrace();
			baseOutDTO.setErrorCode("0032");
			baseOutDTO.setErrorMsg("数据异常：订单号："+order.getOilcardOrdernum());
			return 	baseOutDTO;
		}
	}else{
		oilcardRechargeInfo=oilcardOrderService.findOilReChargeOrderInfo(oilCardChargeId);
	}
	//判断油卡充值状态，为充值请求失败或者充值失败才可进行补充值
	if( oilcardRechargeInfo.getRechargeStatus()==null||1 == oilcardRechargeInfo.getRechargeStatus() || 3 == oilcardRechargeInfo.getRechargeStatus()){
	
		baseOutDTO.setErrorCode("0032");
		baseOutDTO.setErrorMsg("充值已请求成功，不能再补充充值！");
		return 	baseOutDTO;
	}
	
	//判断补充值状态位 为1时（未发起充值请求才可进行补充值）
	if("2".equals(oilcardRechargeInfo.getAddedValueStatus())){
		
		baseOutDTO.setErrorCode("0032");
		baseOutDTO.setErrorMsg("已发起充值请求，不能再补充充值！");
		return 	baseOutDTO;
	}
	//补充值订单
	YsOilcardRepOrder oilcardRepOrder = new YsOilcardRepOrder();
	oilcardRepOrder.setOilcardRechargeId(oilcardRechargeInfo.getId());
	if(StringUtils.isBlank(oilcardNumber)&&StringUtils.isBlank(oilcardOwner)&&StringUtils.isBlank(oilcardType)){
		oilcardRepOrder.setOilcardNumber(order.getOilcardNumber());
		oilcardRepOrder.setOilcardOwner(order.getOilcardOwner());
		oilcardRepOrder.setOilcardType(order.getOilcardType());
	}else if(StringUtils.isNotBlank(oilcardNumber)&&StringUtils.isNotBlank(oilcardOwner)&&StringUtils.isNotBlank(oilcardType)){
		
		oilcardRepOrder.setOilcardNumber(oilcardNumber);
		oilcardRepOrder.setOilcardOwner(oilcardOwner);
		oilcardRepOrder.setOilcardType(oilcardType);
	}else{
		logger.info("参数传入错误  oilcardNumber="+oilcardNumber+"  oilcardOwner="+oilcardOwner+"  oilcardType="+oilcardType);
		
		baseOutDTO.setErrorCode("0032");
		baseOutDTO.setErrorMsg("参数不正确，请核对请求参数！");
		return 	baseOutDTO;
	}
	oilcardRepOrder.setOilcardPhone(order.getOilcardPhone());
	oilcardRepOrder.setOilcardOrderId(SysUtils.Object2Long(orderId));
	oilcardRepOrder.setOperationMember(userName);
	Map<String,String> suc = null;
	try {
		 suc = oilcardOrderService.insertReplenish(oilcardRepOrder,order,oilcardRechargeInfo);
	} catch (Exception e) {
		e.printStackTrace();
		logger.error("补充值请求报错："+e);
		baseOutDTO.setErrorCode("0032");
		baseOutDTO.setErrorMsg("补充充值失败！"+e.getMessage());
		return 	baseOutDTO;
	}
	if(suc==null){
		baseOutDTO.setErrorCode("0032");
		baseOutDTO.setErrorMsg("补充充值失败！");
		return 	baseOutDTO;
		
	}
	if( "0000".equals(suc.get("code"))){
		logger.info("补充值请求返回成功！");
		baseOutDTO.setErrorCode(Rescode.SUCCESS, "补充值请求返回成功");
		return 	baseOutDTO;
	
	}else{
		logger.info("补充值请求返回不成功！");
		baseOutDTO.setErrorCode("0032");
		baseOutDTO.setErrorMsg(suc.get("msg"));
		return 	baseOutDTO;
		
	}	
	
	
}

@ResponseBody
@RequestMapping("toRecharge")
public BaseOutDTO oilCardRecharge(String oilcardOrderId){
	BaseOutDTO baseOutDTO=new BaseOutDTO();
	try {
		oilcardOrderService.oilCardRecharge(oilcardOrderId);
	} catch (Exception e) {
		//TODO
		e.printStackTrace();
		baseOutDTO.setErrorCode("0032");
		baseOutDTO.setErrorMsg("操作异常");
		return 	baseOutDTO;
	}
	baseOutDTO.setErrorCode(Rescode.SUCCESS, "操作成功");
	return baseOutDTO;
}




	
}
